﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GoogleMapLibrary;

public partial class _Default : System.Web.UI.Page
{
    //WebUserControl1.GoogleMap
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            WebUserControl1.GoogleMap.Width = "500px";
            WebUserControl1.GoogleMap.Height = "500px";
            WebUserControl1.GoogleMap.MapType = GoogleMapObject.GoogleMapType.ROADMAP;// GoogleMapType.
            WebUserControl1.GoogleMap.ZoomLevel = 13;
            WebUserControl1.GoogleMap.CenterPoint = new GooglePoint(43.66675, -79.4042);
            //WebUserControl1.GoogleMap.Panorama = new StreetViewPanorama("300px", "300px", "600px", "8px");
            CustomControl control = new CustomControl();
            control.position = CustomControl.PositionControl.RIGHT_TOP;
            control.BackgroundColor = "white";
            control.BorderStyle = "solid";
            control.BorderWidth = "2px";
            control.Cursor = "pointer";
            control.TextAlign = "center";
            control.Title = "Click to set the map to Home";

            control.FontFamily = "Arial,sans-serif";
            control.FontSize = "12px";
            control.PaddingLeft = "4px";
            control.PaddingRight = "4px";
            control.InnerHTML = "Home";

            //CustomControl control1 = new CustomControl();
            //control1.position = CustomControl.PositionControl.RIGHT_BOTTOM;
            //control1.BackgroundColor = "white";
            //control1.BorderStyle = "solid";
            //control1.BorderWidth = "2px";
            //control1.Cursor = "pointer";
            //control1.TextAlign = "center";
            //control1.Title = "Click to set the map to (0,0)";

            //control1.FontFamily = "Arial,sans-serif";
            //control1.FontSize = "12px";
            //control1.PaddingLeft = "4px";
            //control1.PaddingRight = "4px";
            //control1.InnerHTML = "Home1";


            //control1.Clicked += new CustomControl.EventHandlerForControl(control_Clicked1);
            WebUserControl1.GoogleMap.AddControl(control);
            //control.Clicked += new CustomControl.EventHandlerForControl(control_Clicked);
           // WebUserControl1.GoogleMap.AddControl(control1);
            //control1.Clicked += new CustomControl.EventHandlerForControl(control1_Clicked);
            //Style style1 = new Style();

            //style1.FeatureType = Style.MapTypeStyleFeatureType.all;
            //style1.saturation = -80;

            //Style style2 = new Style();

            //style2.FeatureType = Style.MapTypeStyleFeatureType.poi_park;
            //style2.hue = "#ff0022";
            //style2.saturation = 40;

            //WebUserControl1.GoogleMap.AddStyle(style1);
            //WebUserControl1.GoogleMap.AddStyle(style2);

            WebUserControl1.GoogleMap.MouseClick += new GoogleMapObject.EventHandlerForGoogleMapMouse(GoogleMap_MouseClick);
            WebUserControl1.GoogleMap.MarkerClicked += new GoogleMapObject.EventHandlerForMarker(GoogleMap_MarkerClicked);


            //GooglePoint GP1 = new GooglePoint();
            //GP1.ID = "GP1";
            //GP1.Latitude = 43.65669;
            //GP1.Longitude = -79.44268;
            //GP1.InfoHTML = "This is point 1";
            //GP1.Draggable = true;
            //GooglePoint GP2 = new GooglePoint();
            //GP2.ID = "GP2";
            //GP2.Latitude = 43.66619;
            //GP2.Longitude = -79.44268;
            //GP2.InfoHTML = "This is point 2";
           
            //GooglePoint GP3 = new GooglePoint();
            //GP3.ID = "GP3";
            //GP3.Latitude = 43.67689;
            //GP3.Longitude = -79.43270;
            //GP3.InfoHTML = "This is point 3";

            //WebUserControl1.GoogleMap.Markers.Add(GP1);
            //WebUserControl1.GoogleMap.Markers.Add(GP2);
            //WebUserControl1.GoogleMap.Markers.Add(GP3);
            ////Create Polylines between points GP1, GP2 and GP3.
            GooglePolyline PL1 = new GooglePolyline();
            PL1.ID = "PL1";
            //Give Hex code for line color
            PL1.ColorCode = "#0000FF";
            //Specify width for line
            PL1.Width = 5;

            //PL1.Markers.Add(GP1);
            //PL1.Markers.Add(GP2);
            //PL1.Markers.Add(GP3);

            WebUserControl1.GoogleMap.Polylines.Add(PL1);
            //GooglePoint GP1 = new GooglePoint();
            //GP1.ID = "GP1";
            //GP1.Latitude = 43.66675;
            //GP1.Longitude = -79.4042;

            //GooglePoint GP2 = new GooglePoint();
            //GP2.ID = "GP2";
            //GP2.Latitude = 43.67072;
            //GP2.Longitude = -79.38677;

            //GooglePoint GP3 = new GooglePoint();
            //GP3.ID = "GP3";
            //GP3.Latitude = 43.66706;
            //GP3.Longitude = -79.37879;

            //GooglePoint GP4 = new GooglePoint();
            //GP4.ID = "GP4";
            //GP4.Latitude = 43.66135;
            //GP4.Longitude = -79.383;

            //GooglePoint GP5 = new GooglePoint();
            //GP5.ID = "GP5";
            //GP5.Latitude = 43.65787;
            //GP5.Longitude = -79.40016;

            //GooglePoint GP6 = new GooglePoint();
            //GP6.ID = "GP6";
            //GP6.Latitude = 43.66066;
            //GP6.Longitude = -79.40943;

            //GooglePoint GP7 = new GooglePoint();
            //GP7.ID = "GP7";
            //GP7.Latitude = 43.66656;
            //GP7.Longitude = -79.40445;

            ////Create Polygon using above points
            //GooglePolygon PG1 = new GooglePolygon();
            //PG1.ID = "PG1";
            ////Give Hex code for line color
            //PG1.FillColor = "#0000FF";
            //PG1.FillOpacity = 0.4;
            //PG1.StrokeColor = "#0000FF";
            //PG1.StrokeOpacity = 1;
            //PG1.StrokeWeight = 2;

            //PG1.Markers.Add(GP1);
            //PG1.Markers.Add(GP2);
            //PG1.Markers.Add(GP3);
            //PG1.Markers.Add(GP4);
            //PG1.Markers.Add(GP5);
            //PG1.Markers.Add(GP6);
            //PG1.Markers.Add(GP7);

            ////Add polygon to GoogleMap object
            //WebUserControl1.GoogleMap.Polygons.Add(PG1);
        }
    }
    
    void control1_Clicked()
    {
       
            
    }

    void GoogleMap_MarkerClicked(string pID)
    {
        //lblZoomLevel.Text = pID;
        if (WebUserControl1.GoogleMap.Markers[pID].InfoWindow.StatusOpened == false)
        {
            for (int i = 0; i < WebUserControl1.GoogleMap.Markers.Count; i++)
                WebUserControl1.GoogleMap.Markers[i].InfoWindow.StatusOpened = false;
            WebUserControl1.GoogleMap.Markers[pID].InfoWindow.StatusOpened = true;
        }
    }    
   
    void GoogleMap_MouseClick(double latitude, double longitude)
    {
        Marker GP = new Marker();
        GP.ID = WebUserControl1.GoogleMap.Markers.Count.ToString();
        GP.IconImage = "Icons/1.png";
        GP.Position.Latitude = latitude;
        GP.Position.Longitude = longitude;
        GP.InfoWindow.Text = "This is marker " + GP.ID;
        WebUserControl1.GoogleMap.Markers.Add(GP);
        WebUserControl1.GoogleMap.Polylines["PL1"].ArrayPoint.Add(GP.Position);
    }

    //void control_Clicked()
    //{
    //    //WebUserControl1.GoogleMap.CenterPoint.Latitude = 41.850033;
    //    //WebUserControl1.GoogleMap.CenterPoint.Longitude = -87.6500523;
    //    WebUserControl1.GoogleMap.Polygons["PG1"].ArrayPoint["GP1"].Latitude += 0.001;
    //}
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Marker GP = new Marker();
        GP.Address = txtAddress.Text;
        GP.ID = WebUserControl1.GoogleMap.Markers.Count.ToString();
        //GeocodeAddress() function will geocode address and set Latitude and Longitude of GP(GooglePoint) to it's respected value.
        if (GP.GeocodeAddress())
        {
            //Once GP is geocoded, you can add it to google map.
            GP.InfoWindow.Text = GP.Address;
            //Set GP as center point.
            WebUserControl1.GoogleMap.CenterPoint = GP.Position.Clone();
            //Clear any existing
            WebUserControl1.GoogleMap.Markers.Clear();
            //GP.Draggable = true;
            //Add geocoded GP to GoogleMapObject
            WebUserControl1.GoogleMap.Markers.Add(GP);
            WebUserControl1.GoogleMap.ZoomLevel = 15;

        }
        else
        {
            //Show error here.
        }
    }

}